package com.zjt.web.api;

import com.zjt.entity.ClientUser;
import com.zjt.service.IStaticParameterService;
import com.zjt.service.user.IClientUserService;
import com.zjt.web.api.vo.base.StateVO;
import com.zjt.web.api.vo.base.UserVO;
import com.zjt.web.api.vo.request.UserLoginReqVO;
import com.zjt.web.api.vo.request.UserRegiestReqVO;
import com.zjt.web.api.vo.response.UserLoginRespVO;
import com.zjt.web.api.vo.response.UserRegiestRespVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-14
 * Time: AM10:51
 * 用户接口 , 包括：
 * 1. 注册
 * 2. 登陆
 */

@Controller
@RequestMapping(value = "/api/user")
public class UserAPIController {
    private static Logger logger = LoggerFactory.getLogger(UserAPIController.class);
    private IClientUserService clientUserService;
    private IStaticParameterService staticParameterService;
    @Autowired
    public void setClientUserService(IClientUserService clientUserService) {
        this.clientUserService = clientUserService;
    }
    @Autowired
    public void setStaticParameterService(IStaticParameterService staticParameterService) {
        this.staticParameterService = staticParameterService;
    }
    /**
     * 用户注册
     *用户初始化时已经包含注册，将来开发注册功能时，此功能仅为完善资料
     * @param userRegiestReqVO
     * @return
     */
    @RequestMapping(value = {"regiest.json", ""})
    @ResponseBody
    public UserRegiestRespVO regiest(UserRegiestReqVO userRegiestReqVO) {
        logger.info("/api/user/regiest request parms = " + userRegiestReqVO.toString());
        //TODO 进行请求参数的解析
        UserVO userVO = new UserVO();
        StateVO stateVO = new StateVO();
        ClientUser clientUser = null;
        try
        {
            if (this.clientUserService.checkExistByUserId(userRegiestReqVO.getUserId()))
            {
                clientUser = this.clientUserService.updateClientUser(userRegiestReqVO.getUserId(),userRegiestReqVO.getNickName(),userRegiestReqVO.getEmail(),userRegiestReqVO.getPasswd(),
                        userRegiestReqVO.getMdn(),userRegiestReqVO.getSex(),null,null,null);
                stateVO.setMsg("更新用户资料成功");
                stateVO.setCode(0);
            }else  //不存在
            {
                clientUser = this.clientUserService.registerClientUser(userRegiestReqVO.getNickName(),userRegiestReqVO.getEmail(),
                        userRegiestReqVO.getPasswd(),userRegiestReqVO.getMdn(),userRegiestReqVO.getSex(),null,null,null);
                logger.info("注册用户id"+clientUser.getId());
                stateVO.setCode(0);
                stateVO.setMsg("新用户注册成功"+clientUser.getId());
            }
            if (clientUser!=null)
            {
                userVO.setImei(clientUser.getImei());
                userVO.setPasswd(clientUser.getPasswd());
                userVO.setPlatform(clientUser.getPlatform());
                userVO.setUserId(clientUser.getId());
                userVO.setVersion(clientUser.getVersion());
                userVO.setEmail(clientUser.getEmail());
                userVO.setMdn(clientUser.getMdn());
                userVO.setSex(clientUser.getSex());
                userVO.setNickName(clientUser.getNickName());
            } else
            {
                stateVO.setCode(-1);
                stateVO.setMsg("注册失败");
            }
        }catch (Exception ee){
            logger.error("注册异常"+ee.getMessage());
        }
        UserRegiestRespVO userRegiestRespVO = new UserRegiestRespVO();
        userRegiestRespVO.setStateVO(stateVO);
        userRegiestRespVO.setUserVO(userVO);
        //TODO 进行业务逻辑的处理 ， 封装应答数据
        logger.info("/api/user/regiest response result = " + userRegiestRespVO.toString());
        return userRegiestRespVO;
    }

    /**
     * 用户登陆
     * 用户初始化时已经包含登陆，将来开发登陆功能时，此功能仅把权限放开即可
     * @param userLoginReqVO
     * @return
     */
    @RequestMapping(value = {"login.json", ""})
    @ResponseBody
    public UserLoginRespVO login(UserLoginReqVO userLoginReqVO) {
        logger.info("/api/user/login request parms = " + userLoginReqVO.toString());
        //TODO 进行请求参数的解析
        UserVO userVO = new UserVO();
        StateVO stateVO = new StateVO();
        ClientUser clientUser = null;
        try
        {
            clientUser = this.clientUserService.getClientUserByUserName(userLoginReqVO.getUserName().trim());
            if (clientUser!=null)
            {
                logger.info("clientUser="+clientUser.toString());
                if (clientUser.getPasswd().equalsIgnoreCase(userLoginReqVO.getPasswd())==true)
                {
                        logger.info("密码验证成功，开始构造返回对像");
                        userVO.setImei(clientUser.getImei());
                        userVO.setPasswd(clientUser.getPasswd());
                        userVO.setPlatform(clientUser.getPlatform());
                        userVO.setUserId(clientUser.getId());
                        userVO.setVersion(clientUser.getVersion());
                        userVO.setEmail(clientUser.getEmail());
                        userVO.setMdn(clientUser.getMdn());
                        userVO.setSex(clientUser.getSex());
                        userVO.setNickName(clientUser.getNickName());
                } else
                {
                    stateVO.setCode(-1);
                    stateVO.setMsg("登陆失败，密码错误");
                }
            } else
            {
                stateVO.setCode(-1);
                stateVO.setMsg("登陆失败,用户不存在");
            }
        }catch (Exception e){
            logger.error("登陆异常"+e.getMessage());
        }

        UserLoginRespVO userLoginRespVO = new UserLoginRespVO();
        //TODO 进行业务逻辑的处理 ， 封装应答数据
        userLoginRespVO.setStateVO(stateVO);
        userLoginRespVO.setUserVO(userVO);
        logger.info("/api/user/login response result = " + userLoginRespVO.toString());
        return userLoginRespVO;
    }
}
